package com.samoiloff.ui.scroll.resize.model {
	import com.samoiloff.ui.list.smart.view.SmartListView;
	import caurina.transitions.Tweener;

	import com.samoiloff.framework.model.AbstractModel;
	import com.samoiloff.ui.scroll.resize.ResizeScroll;
	import com.samoiloff.ui.scroll.resize.view.ResizeScrollView;

	import flash.events.Event;

	/**
	 * @author Samoiloff
	 */
	public class ResizeScrollModel extends AbstractModel {
		
		public static const SCROLL_POSITION_CHANGED		: String = "scrollPositionChanged";
		public static const MAX_SCROLL_POSITION_CHANGED	: String = "maxScrollPositionChanged";
		public static const HEIGHT_CHANGED				: String = "heightChanged";
		
		private var _scrollPosition		: Number;
		private var _maxScrollPosition	: Number;
		
		private var _height				: Number;		
		
		private var _lineScrollSize		: Number = 10;
		private var _minHeight			: Number = 30;
		
		public function ResizeScrollModel(component:ResizeScroll, view:ResizeScrollView) {
			super(component, view);
			
			_height = 100;
			_maxScrollPosition = 100;
			_scrollPosition = 0;
			complete();
		}

		public function get scrollPosition(): Number {
			return _scrollPosition;
		}
		
		public function set scrollPosition(scrollPosition: Number): void {
			if (scrollPosition < 0) {
				scrollPosition = 0;
			}
			if (scrollPosition > _maxScrollPosition) {
				scrollPosition = _maxScrollPosition;
			}
			if (scrollPosition != _scrollPosition) {
				_scrollPosition = scrollPosition;
				dispatchEvent(new Event(SCROLL_POSITION_CHANGED));
			}
		}
		
		public function get maxScrollPosition(): Number {
			return _maxScrollPosition;
		}
		
		public function set maxScrollPosition(maxScrollPosition: Number): void {
			if (maxScrollPosition < 0) {
				maxScrollPosition = 0;
			}
			
			if (_maxScrollPosition != maxScrollPosition) {
				_maxScrollPosition = maxScrollPosition;
				if (_scrollPosition > _maxScrollPosition) {
					_scrollPosition = _maxScrollPosition;
				}
				dispatchEvent(new Event(MAX_SCROLL_POSITION_CHANGED));
			}
		}
		
		public function get height(): Number {
			return _height;
		}
		
		public function set height(height: Number): void {
			
			if (height < _minHeight) {
				height = _minHeight;
			}
			if (height != _height) {
				_height = height;
				dispatchEvent(new Event(HEIGHT_CHANGED));
			}
		}
		
		public function get view(): ResizeScrollView {
			return _view as ResizeScrollView;
		}
		
		public function get lineScrollSize(): Number {
			return _lineScrollSize;
		}
		
		public function get minHeight(): Number {
			return _minHeight;
		}
		
		public function animateScroll(scrollPosition:Number):void {
			Tweener.removeTweens(this);
			Tweener.addTween(this, {scrollPosition:scrollPosition, time:.5, transition:"easeOutCubic"});
		}
	}
}
